/*
* Copyright (c) 2007, Nubo Technologies
* 
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.nubotech.gwt.oss.browser.client.ui;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.nubotech.gwt.oss.client.auth.AuthenticationListener;
import com.nubotech.gwt.oss.client.auth.AuthenticationManager;
import com.nubotech.gwt.oss.client.auth.Credential;

/**
 *
 * @author jonnakkerud
 */
public class RegisterPanel extends VerticalPanel {
    private TextBox email;
    private PasswordTextBox pass;

    private TextBox account;
    private PasswordTextBox key;
    
    private RadioButton rb0;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    
    private TextBox credentialTb;
    
    /** Creates a new instance of RegisterPanel */
    public RegisterPanel() {
        initGui();
    }
    
    private void initGui() {        
        FlexTable table = new FlexTable();
        table.setCellSpacing(5);
        add(table);
        
        Label emailLabel = new Label("Email:");
        email = new TextBox();
        table.setWidget(0, 0, emailLabel);
        table.setWidget(0, 1, email);
        
        Label passLabel = new Label("Password:");
        pass = new PasswordTextBox();
        table.setWidget(1, 0, passLabel);
        table.setWidget(1, 1, pass);        
        
        Label accountLabel = new Label("Account:");
        account = new TextBox();
        table.setWidget(2, 0, accountLabel);
        table.setWidget(2, 1, account);
        
        Label keyLabel = new Label("Secret key:");
        key = new PasswordTextBox();
        table.setWidget(3, 0, keyLabel);
        table.setWidget(3, 1, key);
        
        // credentials
        // bucket, key
        table.setText(4,0, "credential (<bucket>:<key>):");
        credentialTb = new TextBox();
        table.setWidget(4, 1, credentialTb);
               
        // Make some radio buttons, all in one group.
        rb0 = new RadioButton("myRadioGroup", "Do not store credentials");
        table.setWidget(5, 0, rb0);
        rb1 = new RadioButton("myRadioGroup", "Store credentials locally");
        //table.setWidget(6, 0, rb1);
        rb2 = new RadioButton("myRadioGroup", "Store credentials remotely");
        table.setWidget(6, 0, rb2);
        rb3 = new RadioButton("myRadioGroup", "Existing credential");
        table.setWidget(7, 0, rb3);
        
        rb0.setValue(true);
    }
    
    protected void submit() {
        AuthenticationManager manager = AuthenticationManager.instance();
        Credential credential = new Credential();
        credential.setEmail(email.getText());
        credential.setPass(pass.getText());
        credential.setAccount(account.getText());
        credential.setSecretKey(key.getText());
        if (rb3.getValue()) {
            String s[] = credentialTb.getText().split(":");
            manager.loginRemote(email.getText(), pass.getText(), s[0], s[1], new AuthenticationListener() {
                public void loginSuccess(Credential credential) {
                    History.newItem("");
                }

                public void loginFail(String err) {
                    Window.alert("Registration failed " + err);
                }
            });
        }
        else {
            manager.update(credential, getCheckedButton());
        }
    }

    private AuthenticationManager.Storage getCheckedButton() {
        AuthenticationManager.Storage storage = null;
        storage = AuthenticationManager.Storage.NONE;
        if (rb1.getValue()) {
            storage = AuthenticationManager.Storage.LOCAL;
        }
        else if (rb2.getValue()) {
            storage = AuthenticationManager.Storage.REMOTE;
        }       
        return storage;
    }
    
}
